(function (exports) {
    var appId = null;
    exports.WeChatUtil = {
        SceneType: {
            Friend: 0,
            Round: 1
        },
        isInstalled: function () {
            if (cc.sys.os === cc.sys.OS_IOS) {
                return jsb.reflection.callStaticMethod('WXUtil', 'isWXAppInstalled');
            } else if (cc.sys.os === cc.sys.OS_ANDROID) {
                return jsb.reflection.callStaticMethod(packageUri + '/utils/WeixinUtil', 'isWXAppInstalled', '()I');
            }
            return false;
        },
        getAppid: function () {
            if (cc.sys.os === cc.sys.OS_IOS) {
                return jsb.reflection.callStaticMethod('WXUtil', 'getWeixinAppId');
            } else if (cc.sys.os === cc.sys.OS_ANDROID) {
                if (window.nativeVersion >= '4.0.0') {
                    return jsb.reflection.callStaticMethod(packageUri + '/utils/WeixinUtil', 'getAppId', '()Ljava/lang/String;');
                } else {
                    return jsb.reflection.callStaticMethod(packageUri + '/Global', 'getWeixinAppId', '()Ljava/lang/String;');
                }
            }
            return '';
        },
        setWxPayCode: function (code) {
            if (cc.sys.os == cc.sys.OS_ANDROID) {
                jsb.reflection.callStaticMethod(packageUri + '/Global', 'setWxPayCode', '(Ljava/lang/String;)V', code);
            } else {
                jsb.reflection.callStaticMethod('WXUtil', 'setWxPayCode:', code);
            }
        },
        getWxPayCode: function () {
            var wxPayCode = (cc.sys.os == cc.sys.OS_IOS
                ? jsb.reflection.callStaticMethod('WXUtil', 'getWxPayCode')
                : jsb.reflection.callStaticMethod(packageUri + '/Global', 'getWxPayCode', '()Ljava/lang/String;'));
            return wxPayCode;
        },
        redirectLogin: function (key, data) {
            WeChatUtil.key = key;
            WeChatUtil.data = data;
            if (cc.sys.os == cc.sys.OS_IOS)
                jsb.reflection.callStaticMethod('WXUtil', 'redirectToWeixinLogin');
            else
                jsb.reflection.callStaticMethod(packageUri + '/utils/WeixinUtil', 'redirectToWeixinLogin', '()V');
            if (window.waitWxCodeInterval) {
                clearInterval(window.waitWxCodeInterval);
                window.waitWxCodeInterval = null;
            }
            window.waitWxCodeInterval = setInterval(this.waitCodeInterval.bind(this), 100);
        },
        waitCodeInterval: function () {
            var code = this.getWXLoginCode();
            if (!isNullString(code)) {
                clearInterval(window.waitWxCodeInterval);
                window.waitWxCodeInterval = null;
                eventManager.dispatchEvent(EventKey.ReceiveWechatCode, {
                    code: code,
                    key: WeChatUtil.key,
                    data: WeChatUtil.data
                });
                WeChatUtil.key = null;
                WeChatUtil.data = null;
            }
        },
        getWXLoginCode: function () {
            return appId = (cc.sys.os == cc.sys.OS_IOS
                ? jsb.reflection.callStaticMethod('WXUtil', 'getWXLoginCode')
                : jsb.reflection.callStaticMethod(packageUri + '/utils/WeixinUtil', 'getWXLoginCode', '()Ljava/lang/String;'));
        },
        setWXLoginCode: function (code) {
            return appId = (cc.sys.os == cc.sys.OS_IOS
                ? jsb.reflection.callStaticMethod('WXUtil', 'setWXLoginCode:', code)
                : jsb.reflection.callStaticMethod(packageUri + '/utils/WeixinUtil', 'setWXLoginCode', '(Ljava/lang/String;)V', code));
        },
        shareImageToWX: function (type) {
            var nameJPG = (cc.sys.os == cc.sys.OS_IOS ? 'storage/' : '') + 'res/mainScene/share/share_game.jpg';
            if (cc.sys.os == cc.sys.OS_ANDROID) {
                var isFriend = type;
                jsb.reflection.callStaticMethod(
                    packageUri + '/utils/WeixinUtil',
                    'sharePic',
                    '(Ljava/lang/String;Z)V',
                    nameJPG,
                    isFriend
                );
            } else if (cc.sys.os == cc.sys.OS_IOS) {
                var isFriend = type ? 1 : 0;
                jsb.reflection.callStaticMethod(
                    'WXUtil',
                    'sharePic:imageName:sceneType:',
                    jsb.fileUtils.getWritablePath(),
                    nameJPG,
                    isFriend
                );
            }
        },
        captureAndShareToPyq: function (node, depthStencilFormat) {
            var winSize = cc.director.getWinSize();
            var texture = new cc.RenderTexture(winSize.width, winSize.height, null, depthStencilFormat);
            if (!texture)
                return;

            texture.retain();

            texture.setAnchorPoint(0, 0);
            texture.begin();
            node.visit();
            texture.end();

            var time = timestamp2time(Math.round((new Date()).valueOf() / 1000));
            time = time.replace(/[\s:-]+/g, '_');
            var nameJPG = "ss-" + time + ".jpg";

            if (cc.sys.os == cc.sys.OS_ANDROID) {
                texture.saveToFile(nameJPG, cc.IMAGE_FORMAT_JPEG, false, function (renderTexture, str) {
                    texture.release();
                    jsb.reflection.callStaticMethod(
                        packageUri + "/utils/WeixinUtil",
                        "sharePic",
                        "(Ljava/lang/String;Z)V",
                        nameJPG,
                        true
                    );
                });
            }
            else if (cc.sys.os == cc.sys.OS_IOS) {
                texture.saveToFile(nameJPG, cc.IMAGE_FORMAT_JPEG, true, function (renderTexture, str) {
                    texture.release();
                    jsb.reflection.callStaticMethod(
                        "WXUtil",
                        "sharePic:imageName:sceneType:",
                        jsb.fileUtils.getWritablePath(),
                        nameJPG,
                        1
                    );
                });
            }
        },
        captureAndShareToWX: function (node, depthStencilFormat, sceneType) {
            var winSize = cc.director.getWinSize();
            var texture = new cc.RenderTexture(winSize.width, winSize.height, null, depthStencilFormat);
            if (!texture)
                return;

            texture.retain();

            texture.setAnchorPoint(0, 0);
            texture.begin();
            node.visit();
            texture.end();

            var time = timestamp2time(Math.round((new Date()).valueOf() / 1000));
            time = time.replace(/[\s:-]+/g, '_');
            var nameJPG = 'ss-' + time + '.jpg';

            if (cc.sys.os == cc.sys.OS_ANDROID) {
                texture.saveToFile(nameJPG, cc.IMAGE_FORMAT_JPEG, false, function (renderTexture, str) {
                    texture.release();
                    jsb.reflection.callStaticMethod(
                        packageUri + '/utils/WeixinUtil',
                        'sharePic',
                        '(Ljava/lang/String;Z)V',
                        nameJPG,
                        (sceneType ? true : false)
                    );
                });
            } else if (cc.sys.os == cc.sys.OS_IOS) {
                texture.saveToFile(nameJPG, cc.IMAGE_FORMAT_JPEG, true, function (renderTexture, str) {
                    texture.release();
                    jsb.reflection.callStaticMethod(
                        'WXUtil',
                        'sharePic:imageName:sceneType:',
                        jsb.fileUtils.getWritablePath(),
                        nameJPG,
                        (sceneType ? 1 : 0)
                    );
                });
            }
        },
        /**
         * 截图
         * @param {cc.Node} node
         * @param {Number} [sceneType]
         * @public
         */
        shareCapture: function (node, sceneType) {
            sceneType = sceneType || WeChatUtil.SceneType.Friend;
            if(sceneType == WeChatUtil.SceneType.Round){
                this.captureAndShareToPyq(node,0x88F0);
            }else{
                this.captureAndShareToWX(node,0x88F0);
            }
        },
        shareText: function (text, sceneType, transaction) {
            if (!cc.sys.isNative) {
                return;
            }
            if (cc.sys.os == cc.sys.OS_IOS) {
                jsb.reflection.callStaticMethod(
                    'WXUtil',
                    'shareText:sceneType:',
                    text,
                    (sceneType ? 1 : 0)
                );
            } else {
                jsb.reflection.callStaticMethod(
                    packageUri + '/utils/WeixinUtil',
                    'shareText',
                    '(Ljava/lang/String;ZLjava/lang/String;)V',
                    text,
                    (sceneType ? true : false),
                    transaction
                );
            }
        },
        shareUrl: function (url, title, description, sceneType, transaction) {
            if (cc.sys.os == cc.sys.OS_IOS) {
                jsb.reflection.callStaticMethod(
                    'WXUtil',
                    'shareUrl:title:description:sceneType:',
                    url,
                    title,
                    description,
                    (sceneType ? 1 : 0)
                );
            } else {
                jsb.reflection.callStaticMethod(
                    packageUri + '/utils/WeixinUtil',
                    'shareUrl',
                    '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZLjava/lang/String;)V',
                    url,
                    title,
                    description,
                    (sceneType ? true : false),
                    transaction
                );
            }
        },
        beginWxPay: function (cid) {
            showLoading('正在跳转微信支付...');
            var that = this;
            var data = {
                appid: WXA[gameData.appId],
                playerid: gameData.uid,
                unionid: gameData.unionid,
                area: gameData.parent_area,
                cid: cid,
                timestamp: getCurTimestamp(),
                wechatOrderKey: '78445A3D4370C7B35318D1FF348EA63B'
            };
            data.sign = Crypto.MD5('' + data.appid + data.playerid + data.unionid + data.area + data.cid + data.player + data.timestamp);
            NetUtils.httpPost('http://'+app.payUrl+'/payServer/game/wechatOrder', data,
                function (data) {
                    hideLoading();
                    if (data['prepay_id']) {
                        that.wxPay(data['prepay_id']);
                    } else {
                        showAlert('支付请求失败,请检查网络');
                    }
                },
                function (error) {
                    hideLoading();
                    showAlert('支付请求失败,请检查网络');
                }
            );
        },
        wxPay: function (prepayId) {
            var nonceStr = randomString(16);
            var packageValue = 'Sign=WXPay';
            var timeStamp = getCurTimestamp();
            if (cc.sys.os == cc.sys.OS_IOS) {
                var str = 'appid=' + WXA[gameData.appId] + '&noncestr=' + nonceStr + '&package=' + packageValue + '&partnerid=' + WXP[gameData.appId] + '&prepayid=' + prepayId + '&timestamp=' + timeStamp + '&key=' + WXK[gameData.appId];
                var ios_sign = Crypto.MD5(str).toUpperCase();
                jsb.reflection.callStaticMethod(
                    'WXUtil',
                    'wxPay:prepayId:package:nonceStr:timeStamp:sign:',
                    WXP[gameData.appId],
                    prepayId,
                    packageValue,
                    nonceStr,
                    timeStamp + '',
                    ios_sign
                );
            } else if (cc.sys.os == cc.sys.OS_ANDROID) {
                var android_sign = Crypto.MD5('appid=' + WXA[gameData.appId] + '&noncestr=' + nonceStr + '&package=' + packageValue + '&partnerid=' + WXP[gameData.appId] + '&prepayid=' + prepayId + '&timestamp=' + timeStamp + '&key=' + WXK[gameData.appId]).toUpperCase();
                jsb.reflection.callStaticMethod(
                    packageUri + '/utils/WeixinUtil',
                    'wxPay',
                    '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V',
                    WXA[gameData.appId],
                    WXP[gameData.appId],
                    prepayId,
                    packageValue,
                    nonceStr,
                    timeStamp + '',
                    android_sign
                );
            }
        },
        setTransResult: function (key, value) {
            if (cc.sys.os == cc.sys.OS_IOS) {
                return jsb.reflection.callStaticMethod(
                    'WXUtil',
                    'setWXOnRespResult:v:',
                    key,
                    value
                );
            } else {
                return jsb.reflection.callStaticMethod(
                    packageUri + '/Global',
                    'setTransResult',
                    '(Ljava/lang/String;Ljava/lang/String;)V',
                    key,
                    value);
            }
        },
        getTransResult: function (key) {
            if (cc.sys.os === cc.sys.OS_IOS) {
                return jsb.reflection.callStaticMethod('WXUtil', 'getWXOnRespResult:', key);
            } else if (cc.sys.OS_ANDROID) {
                return jsb.reflection.callStaticMethod(packageUri + '/utils/WeixinUtil', 'getTransResult', '(Ljava/lang/String;)Ljava/lang/String;', key);
            }
            return '';
        },

        /**
         * 小程序分享
         */
        shareMiniPro: function (url, title, description, texture, miniId, path) {
            if (window.nativeVersion < '4.0.0') {
                return;
            }
            if (cc.sys.os === cc.sys.OS_IOS) {
                jsb.reflection.callStaticMethod(
                    'WXUtil',
                    'shareMiniPro:title:description:hdImagePath:userName:path:miniProgramType:',
                    url,
                    title,
                    description,
                    texture,
                    miniId,
                    path,
                    2
                );
            } else if (cc.sys.os === cc.sys.OS_ANDROID) {
                jsb.reflection.callStaticMethod(
                    packageUri + '/utils/WeixinUtil',
                    'shareMiniPro',
                    '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V',
                    url,
                    title,
                    description,
                    texture,
                    miniId,
                    path,
                    '2'
                );
            }
        },

        setMiniProRoomId: function (roomid) {
            if (window.nativeVersion < '4.0.0') {
                return;
            }
            if (cc.sys.os === cc.sys.OS_IOS) {
                jsb.reflection.callStaticMethod('WXUtil', 'setminiRroRoomId:', roomid);
            } else if (cc.sys.os === cc.sys.OS_ANDROID) {
                jsb.reflection.callStaticMethod(packageUri + '/utils/WeixinUtil', 'setminiRroRoomId', '(Ljava/lang/String;)V', roomid);
            }
        },

        getMiniProRoomId: function () {
            if (window.nativeVersion < '4.0.0') {
                return;
            }
            if (cc.sys.os === cc.sys.OS_IOS) {
                return jsb.reflection.callStaticMethod('WXUtil', 'getminiRroRoomId');
            } else if (cc.sys.os === cc.sys.OS_ANDROID) {
                return jsb.reflection.callStaticMethod(packageUri + '/utils/WeixinUtil', 'getminiRroRoomId', '()Ljava/lang/String;');
            }
            return '';
        }
    };

})(this);